import { Component, OnInit } from "@angular/core";
import { ElevatorRescueBean } from "./bean/elevatorRescue.bean";
import {
  FormBuilder,
  FormGroup,
  Validators,
  FormControl
} from "@angular/forms";
import { NzModalService, NzModalSubject } from "ng-zorro-antd";
import { Router, ActivatedRoute, ParamMap } from "@angular/router";
import { HttpServcie } from "../../../service/http.service";
import { NzNotificationService } from "ng-zorro-antd";
import { Config } from "../../../service/config";
import { CommFuntion } from "../../../service/commFunction";

@Component({
  selector: "app-elevator-rescue",
  templateUrl: "./elevator-rescue.component.html",
  styleUrls: ["./elevator-rescue.component.scss"]
})
/**
 * 应急救援
 */
export class ElevatorRescuePage implements OnInit {
  list: any = []; //返回数据列表
  picList = [];
  total: any;
  elevatorRescueBean: ElevatorRescueBean = new ElevatorRescueBean();
  data: any; //传入详细页面数据
  currentModal: NzModalSubject;
  conf = Config;
  constructor(
    private modalService: NzModalService,
    private router: Router,
    private route: ActivatedRoute,
    private httpServcie: HttpServcie,
    private _notification: NzNotificationService,
    private commFunc: CommFuntion
  ) {}

  ngOnInit() {
    this.elevatorRescueBean.pageSize = 10;
    this.elevatorRescueBean.currentPage = 1;
    this.route.paramMap.subscribe(data => {
      //查询电梯的信息
      this.elevatorRescueBean.elevatorId = data.get("elevatorId");
      this.query();
    });
  }
  //查询
  query() {
    //查询当前电梯救援记录
    this.httpServcie.emergencyRescueList(this.elevatorRescueBean).subscribe(
      data => {
        if (data.status == 200) {
          this.list = data.data.list;
          this.total = data.data.total;
        } else if (data.status === 401) {
          this.router.navigateByUrl("/login");
          return;
        } else {
          // 异常
          this._notification.create("error", "错误提示", data.message);
        }
      },
      error => {
        console.log(JSON.stringify(error));
      }
    );
  }
  //table加载数据时
  displayDataChange($event) {
    this.list = $event;
  }
  //分页
  nzPageSizeChange(event) {
    this.elevatorRescueBean.pageSize = event;
    this.elevatorRescueBean.currentPage = 1;
    this.query();
  }

  //输入框的方法
  nzPageIndexChange(event) {
    this.elevatorRescueBean.currentPage = event;
    this.query();
  }

  //当前页
  nzPageIndexClickChange(event) {
    this.elevatorRescueBean.currentPage = event;
    this.query();
  }
  toDetail(data) {
    this.router.navigate(["/home/rescue-detail", { id: data.id }]);
  }

  add(titleTpl, contentTpl, footerTpl) {}
  //返回
  toBack() {
    window.history.back();
  }

  getPics(pic) {
    if (pic) {
      return pic.split(",");
    } else {
      return [];
    }
  }

  export(id) {
    this.httpServcie.exportEmRsc(id).subscribe(data => {
      if (data.status == 200) {
        this.download(data.data.fileUrl, data.data.fileName);
      } else {
        this._notification.create("error", "出错了", "导出失败");
      }
    });
  }
  showLargeImage(titlepic, contentpic, footerpic, img) {
    this.picList = [];
    this.picList.push(img);
    this.currentModal = this.modalService.open({
      title: titlepic,
      content: contentpic,
      footer: footerpic,
      maskClosable: false,
      width: 800,
      style: {
        top: "20px"
      },
      onOk() {
        console.log("Click ok");
      }
    });
  }

  download(url, name) {
    let link = document.createElement("a");
    let body = document.querySelector("body");

    this.getBlob(Config.IP + Config.API.getUpload + url).then(blob => {
      link.href = window.URL.createObjectURL(blob);
      link.download = name;
      // fix Firefox
      link.style.display = "none";
      body.appendChild(link);
      link.click();
      body.removeChild(link);
      window.URL.revokeObjectURL(link.href);
    });
  }
  getBlob(url) {
    return new Promise(resolve => {
      const xhr = new XMLHttpRequest();
      xhr.open("GET", url, true);
      xhr.responseType = "blob";
      xhr.onload = () => {
        if (xhr.status === 200) {
          resolve(xhr.response);
        }
      };
      xhr.send();
    });
  }
}
